public class MySingleList {
    static class ListNode{
        public int val;//节点的值域
        public ListNode next;//下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//当前链表的头节点
    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    public void display(){
        ListNode multiple = head;
        while (multiple != null){
            System.out.print(multiple.val+" ");
            multiple = multiple.next;
        }
        System.out.println();
    }
    public int size(){
        ListNode multiple = head;
        int count = 0;
        while (multiple != null){
            multiple = multiple.next;
            count++;
        }
        return count;
    }
    public boolean contain(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(cur == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    public void addIndex(int aim,int data){
        if(aim < 0 || aim > size()){
            System.out.println("位置不合法");
            return;
        }
        else if(aim == 0){
            addFirst(data);
            return;
        }
        else if(aim == size()){
            addLast(data);
            return;
        }
        ListNode cur = index(aim);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    public ListNode index(int aim){
        ListNode cur = head;
        int count = 0;
        for (int i = 0; i < size(); i++) {
           if (count == aim-1 && aim-1 != 0){
                   return cur;
            }
           if (aim-1 == 0){
               head = cur;
               return cur;
           }
            cur = cur.next;
            count++;
        }return null;
//        while (aim-1 != 0){
//            cur = cur.next;
//            aim--;
//        }
//        return cur;
    }
    private ListNode searchPrev(int key){
        ListNode cur = head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }return null;
    }
    public void remove(int key){
        if (head == null){
            return;
        }
        if (head.val == key){  //单独删除头节点
            head = head.next;
        }
        ListNode cur = searchPrev(key);
        if (cur == null){
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
}
